package com.sky.service.impl;

import com.sky.dto.DataOverViewQueryDTO;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
       //list集合 先存储开始到结束每天的集合
        List<LocalDate> localDateList = new ArrayList<>();
        localDateList.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            localDateList.add(begin);
        }

        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate localDate : localDateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            //select sum(amount) from orders where order_time > ? and order_time < ? where status = 5
            Double amount  = orderMapper.selectSumAmount(beginTime,endTime);
            amount = amount == null ? 0.0 : amount;
            turnoverList.add(amount);
        }
        

        String stringDate = StringUtils.join(localDateList, ",");
        String stringTurnover = StringUtils.join(turnoverList, ",");
        return TurnoverReportVO.builder()
                .dateList(stringDate)
                .turnoverList(stringTurnover)
                .build();
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //将开始时间 结束时间 之间的天数  装到集合中
        List<LocalDate> localDateList = new ArrayList<>();
        localDateList.add(begin);
        while(!begin.equals(end)){
            begin = begin.plusDays(1);
            localDateList.add(begin);
        }
        String stringDate = StringUtils.join(localDateList, ",");
        List<Integer> totalUserList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();
        for (LocalDate localDate : localDateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer newCount = userMapper.selectSumUser(beginTime, endTime);
            newCount = newCount==null ? 0 : newCount;
            //截止到本天的用户数量
            Integer count =  userMapper.selectSumUser(null,endTime);

           count = count==null ? 0 : count;
           totalUserList.add(count);
           newUserList.add(newCount);
        }
        String stringTotalUser = StringUtils.join(totalUserList, ",");
        String stringNewUser = StringUtils.join(newUserList, ",");

        return UserReportVO.builder()
                .dateList(stringDate)
                .totalUserList(stringTotalUser)
                .newUserList(stringNewUser)
                .build();
    }
}
